import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import {WeaTableNew} from 'comsMobx'
import * as mobx from 'mobx'
const {TableStore} = WeaTableNew;
import * as API_LIST from '../apis/rptHistory';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 报表列表通用store模板，适应于列表+高级查询模式
 */
export class ListStore{
    /** 列表store */
    @observable loading = false;
    @observable title = "";
    dataKey = "";
    shareDataKey = "";
    @observable tableStore = new TableStore();
    @observable shareTableStore = new TableStore();

    /** 高级查询store */
    @observable form = new WeaForm();
    @observable showSearchAd = false;
    @observable condition = [];

     /** 高级查询store */
    @observable formHis = new WeaForm();
    @observable conditionHis = [];

     /** 报表表单 */
    @observable formRp = new WeaForm();
    @observable conditionRp = [];

    /** 顶部Tab组件store */
    topTab = [];
    tabKey = 0;
    @observable topTabCount = {};

    @observable showShareModel = false;
  
    @observable loadingSaveModal = false;
    /**
     * 共享modal参数
     * id  历史表主键
     */
    shareModalParams = {
        id : "",
        title : "",
        rptTypeName : "",
    }

    /** 路由 **/
    router = {};

    //页面类型
    pageType = ''

    @observable hasRight;//权限的标记 4无权限，跳转
    
    constructor() {
        this.setLoading = this.setLoading.bind(this);
        this.setTitle = this.setTitle.bind(this);
        this.setShowSearchAd = this.setShowSearchAd.bind(this);
        this.setFormFields = this.setFormFields.bind(this);
        this.appendFormFields = this.appendFormFields.bind(this);
        this.clearFormFields = this.clearFormFields.bind(this);
        this.historyInfo = this.historyInfo.bind(this);
        this.queryHistoryData = this.queryHistoryData.bind(this);
        this.queryShareData = this.queryShareData.bind(this);
        this.setShowShareModel = this.setShowShareModel.bind(this);
        this.setTabkey = this.setTabkey.bind(this);
        this.setShareModalVisible = this.setShareModalVisible.bind(this);
    }

    @action
    setLoading(bool = false){
        this.loading = bool;
    }
    
    setTitle(title){
        this.title = title;
    }

    setShowSearchAd(bool) {
        this.showSearchAd = bool;
    }

    setFormFields(value){
        this.form.updateFields(value, true);    //true代表完全覆盖方式更新条件值
    }

    appendFormFields(){
        this.form.updateFields(value, false);
    }

    //不同的form
    clearFormFields(key){
        if(key==0){
            this.form.resetConditionValue();//清除查询条件值
        }else if(key==1){
            this.formHis.resetConditionValue();//清除查询条件值
        }
           
    }

    //共享模块参数
    setShareModalVisible(params){
        this.shareModalParams.id = params.id;
        this.shareModalParams.title = params.title;
        this.shareModalParams.rptTypeName = params.rptTypeName;
    }


    //历史记录高级搜索
    historyInfo(params){
        API_LIST.queryHistoryInfo(params).then(data=>{
            if(data.flag == 'success'){
                let {conditions} = data;
                this.conditionHis = conditions;
                this.formHis = new WeaForm();
                this.formHis.initFormFields(conditions); 
            }else{
                if(this.router){
                    this.router.push('main/fna/fnaReportPermission');// main/fna/fnaReportPermission  data.url
                }
            }       
        });
    }

    //历史查询结果
    queryHistoryData(params,flag = true){ 
        this.loading = true;
        API_LIST.queryHistoryData(params).then(data=>{
            if(data.flag == 'success'){
                this.tableStore = new TableStore();
                if(flag){//回到第一页
                    this.tableStore.getDatas(data.datas,1);
                }else{
                    this.tableStore.getDatas(data.datas);
                }
                this.dataKey = data.datas;
                this.loading = false;  
            }else{
                if(this.router){
                    this.router.push('main/fna/fnaReportPermission');// main/fna/fnaReportPermission  data.url
                }
            }
        });
    }

    //查询共享数据
    queryShareData(params){
        this.loading = true;
        API_LIST.queryShareModalDataP(params).then(data =>{
            if(data.flag == 'success'){
                this.shareTableStore.getDatas(data.datas);
                this.shareDataKey = data.datas;
                this.loading = false;
                this.setShowShareModel(true);
            }else{
                if(this.router){
                    this.router.push('main/fna/fnaReportPermission');// main/fna/fnaReportPermission  data.url
                }
            }
        });
    }

    //解析从数据库拿到的查询参数
    parseStringToJson(paramter){
        let json="";
        // let values = paramter.split("&");
        // for(let value of values){
        //     let params = value.split("=");
        //     let jsonString="";
        //     let index=0;
        //     for(let param of params){
        //         if(index==0){
        //             jsonString = jsonString+"\""+param+"\""+":";
        //         }else{
        //             jsonString = jsonString+"\""+param+"\"";
        //         }
        //         index++;
        //     }   
        //     json = json+jsonString+",";
        // }
        // let index = json.lastIndexOf(",");
        // json = "{"+json.substring(0,index)+"}";


        json = JSON.parse(paramter);
        return json;
    }

    //获取报表form
    getModalForm = (params) => {
        this.loadingSaveModal = true;
        params = Object.assign(params,{
            is_multilang_set:true
        });
        API_LIST.getModalForm(params).then(data=>{
            if(data.flag == 'success'){
                let {conditions} = data;
                this.conditionRp = conditions;
                this.formRp = new WeaForm();
                this.formRp.initFormFields(conditions); 
            }     
            this.loadingSaveModal = false; 
        });
    }

    //重置报表表单
    clearformRp = () => {
        this.formRp.resetConditionValue();
    }

    @action
    setShowShareModel(bool){
        this.showShareModel = bool;
    }

    setTabkey(value){
        this.tabKey = value;
    }

    setRouter = (value) => {
        this.router = value;
    }
}